<template>
     <el-form :model="tableForm" :rules="tableRule" ref="tableForm" label-width="120px">

       <el-row>
         <el-col :span="20">
           <el-form-item label="卡号" prop="account">
             <el-input size="small" v-model="tableForm.account" auto-complete="off" placeholder="请输入卡号"></el-input>
           </el-form-item>
         </el-col>
         <el-col :span="4">
           <el-popover placement="right" trigger="click" v-model="visible">
             <Selector @getSelectorData="getSelectorData"></Selector>
             <el-button slot="reference" >选择</el-button>
           </el-popover>
         </el-col>
       </el-row>

       <el-form-item label="药物名称" prop="name">
         <el-input size="small" v-model="tableForm.name" auto-complete="off" placeholder="请输入药物名称"></el-input>
       </el-form-item>

       <el-form-item label="服用时间" prop="createDate">
         <el-date-picker type="date" placeholder="请输入服用时间" v-model="tableForm.createDate" value-format="yyyy-MM-dd"></el-date-picker>
       </el-form-item>

       <el-form-item label="服用说明" prop="info">
         <el-input size="small" type="textarea" v-model="tableForm.info" auto-complete="off" placeholder="请输入服用说明"></el-input>
       </el-form-item>


     </el-form>
</template>

<script>

import {addData, updateData} from '@/api/pharmacy'
import Selector from '../user/selector'

// 表单校验
const tableRule = {
  account: [{ required: true, message: '请输入卡号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入药物名称', trigger: 'blur' }],
};

export default {
  name: 'PharmacyEdit',
  props: {
    formData :{
      type: Object
    }
  },
  data() {
    return {
      tableForm: this.formData,
      tableRule: tableRule,
      visible: false,
    }
  },
  components:{
    Selector
  },
  methods:{
    //表单保存事件
    submitForm() {
      this.$refs["tableForm"].validate(valid => {
        if (valid) {
          if(this.tableForm.id == ""){
            addData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }else{
            updateData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 选择父表数据
    getSelectorData (v) {
      this.visible = false
      this.tableForm.account = v[0].account
      this.tableForm.userId = v[0].id
    },
    clearForm(){
      this.$refs.tableForm.resetFields();
      this.$refs.tableForm.clearValidate();
    }
  }
}
</script>

<style scoped>


</style>
